//
//  MBProgressHUD+Extension.swift
//  MT
//
//  Created by zhang qiang on 2018/11/21.
//  Copyright © 2018年 张强. All rights reserved.
//

import UIKit
import MBProgressHUD

public enum MBProgressHUDMessageLocation : Int {
    case center = 0, bottom = 1000000, top = -1000000
}

public extension MBProgressHUD {
    
    /// 显示loading和文字
    ///
    /// - Parameters:
    ///   - view: 如果传入`nil`,则显示在Window上
    ///   - text: 默认为 `nil`
    static func showLoading(_ view:UIView?, text:String? = nil) {
        showText(text: text, icon: nil, view: view)
    }
    
    static func showProgress(_ view: UIView, text: String?) {
        
        let hud = MBProgressHUD.showAdded(to: view, animated: true)
            hud.mode = .annularDeterminate
            hud.label.text = text
            hud.progress = 0
    }
    
    static func modifyProgressHUD(_ view: UIView, progress: Float, text: String?) {
        let hud = MBProgressHUD(for: view)
        hud?.progress = progress
        hud?.label.text = text
        if progress >= 1 {
           hud?.hide(animated: true, afterDelay: 0.2)
        }
    }
    
    /// 仅显示文本
    ///
    /// - Parameters:
    ///   - text: String
    ///   - location: 默认在底部显示
    ///   - afterDelay: 默认持续2秒
    static func showMessage(_ text:String?, location:MBProgressHUDMessageLocation = .bottom, afterDelay:TimeInterval = 2) {
        
        guard text != nil else {
            return
        }
        
        let view = currentlyWithShow(nil)
        DispatchQueue.main.async {
            let hud = MBProgressHUD.showAdded(to: view, animated: true)
            hud.mode = .text
            hud.label.text = text
            hud.label.textColor = UIColor.white
            hud.label.numberOfLines = 0
            hud.label.font = UIFont.systemFont(ofSize: 18)
            hud.offset = CGPoint(x: 0, y: location.rawValue)
            hud.bezelView.backgroundColor = rgbaColorFromHex(rgb: 0x000000, alpha: 0.8)
            hud.hide(animated: true, afterDelay: afterDelay)
        }
    }
    
    /// 成功HUD
    ///
    ///  text is `nil`, show image
    /// - Parameter text: default `nil`
    static func showSuccess(_ text: String? = nil) {
        let view = currentlyWithShow(nil)
        showText(text: text, icon: "CheckmarkW", view: view)
    }
    
    /// 错误HUD
    ///
    ///  text is `nil` , show image
    /// - Parameter text: default `nil`
    static func showError(_ text: String? = nil ) {
        let view = currentlyWithShow(nil)
        showText(text: text, icon: "ErrorW", view: view)
    }
    
    /// HUD消失
    ///
    /// - Parameter view: `nil` -> window
    static func dismiss(_ view:UIView?) {
        let view = currentlyWithShow(view)
        DispatchQueue.main.async {
            MBProgressHUD.hide(for: view, animated: true)
        }
    }
    
    fileprivate static func showText(text: String? = nil, icon: String?, view:UIView?) {
        let view = currentlyWithShow(view)
        var imageView:UIImageView?
        
        // 拿取图片
        if icon != nil && icon!.count > 0 {
            var image:UIImage?
            image = UIImage.imagePath(named: icon!, bundle: Bundle.MTCategroy)
            if image != nil {
                imageView = UIImageView(image: image)
            }
        }
        
        DispatchQueue.main.async {
            // 避免覆盖
            MBProgressHUD.hide(for: view, animated: true)
            let hud = MBProgressHUD.showAdded(to: view, animated: true)
            hud.bezelView.backgroundColor = rgbaColorFromHex(rgb: 0x000000, alpha: 0.8)
            hud.contentColor = UIColor.white
            if imageView != nil {
                hud.mode = .customView
                hud.customView = imageView
                hud.isSquare = true
                hud.removeFromSuperViewOnHide = true
                hud.hide(animated: true, afterDelay: 2)
            }
            
            if text != nil {
                hud.label.text = text
            }
        }
    }
    
    /// Get view
    fileprivate static func currentlyWithShow(_ view:UIView?) -> UIView {
        
        if view == nil {
            // warning - maybe crash
            var window = UIApplication.shared.delegate?.window!
            if window?.windowLevel != UIWindow.Level.normal {
                let windowArray = UIApplication.shared.windows
                
                for tempWin in windowArray {
                    if tempWin.windowLevel == UIWindow.Level.normal {
                        window = tempWin
                        break
                    }
                }
            }
            return window!
        }
        
        return view!
    }
    
    /// Hexadecimal convert RGBA
    ///
    /// - Parameters:
    ///   - rgb: example: `0xDFDFDF`
    ///   - alpha: default `1.0`
    /// - Returns: `color`
    fileprivate static func rgbaColorFromHex(rgb:Int, alpha: CGFloat = 1.0) -> UIColor {
        
        return UIColor(red: ((CGFloat)((rgb & 0xFF0000) >> 16)) / 255.0,
                       green: ((CGFloat)((rgb & 0xFF00) >> 8)) / 255.0,
                       blue: ((CGFloat)(rgb & 0xFF)) / 255.0,
                       alpha: alpha)
    }
}

